package Test;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

public class Test16 {
    //合法括号判断
    public static boolean chkParenthesis1(String A,int n){
        if(n % 2 != 0){
            return false;
        }
        Stack<Character> stack = new Stack<>();
            for (int i = 0; i < A.length(); i++) {
                if(A.charAt(i) == '('){
                    stack.push(A.charAt(i));
                }else if(A.charAt(i) == ')'){
                    if(stack.isEmpty()){
                        return false;
                    }else{
                        stack.pop();
                    }
                }else{
                    return false;
                }
            }
        if(!stack.isEmpty()){
            return false;
        }
        return true;
    }


//第二种方法，带有字符串长度
    public static boolean chkParenthesis(String A) {
        if(A == null || A.length() == 0){
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < A.length(); i++) {
            if(A.charAt(i) == '('){
                stack.push(A.charAt(i));
            }else if(A.charAt(i) == ')'){
                if(stack.isEmpty()){
                    return false;
                }else{
                    stack.pop();
                }
            }else{
                return false;
            }
        }
        if(!stack.isEmpty()){
            return false;
        }
        return true;
    }
    public static void main(String[] args) {
        String str = "()()a";
        int n = 5;
        boolean str1 = chkParenthesis1(str,n);
        System.out.println(str1);
    }

    //现在给你一个数N求最少需要多少步可以变为Fibonacci数。
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int a = 0;
        int b = 1;
        int c = 1;
        while (c < n){
            a = b;
            b = c;
            c = a + b;
        }
        int d = n - b;
        int e = c - n;
        //int ret = Math.min(d,e);
        System.out.println(d < e ? d : e);
    }
    //插入排序
    public static void insert(int [] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (j = i-1; j >= 0 ; j--) {
                if(array[j] > array[i]){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    public static void main1(String[] args) {
        int [] ret = {12,3,34,56,45};
        insert(ret);
        System.out.println(Arrays.toString(ret));
    }
}
